<template>
  <base-btn
    :title="title"
    @submit="toDeliver"
    @resetForm="resetForm"
    :type="type"
  >
    <el-form label-width="0px" :model="form" :rules="rules" ref="ruleForm">
      <el-form-item label="" prop="target_username">
        <el-autocomplete
          v-model="form.target_username"
          :fetch-suggestions="querySearchAsync"
          placeholder="请输入关键词搜索转交人"
          @select="handleSelect"
          style="width: 100%"
        ></el-autocomplete>
      </el-form-item>
      <el-form-item label="" prop="suggestion">
        <el-input
          v-model="form.suggestion"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 8 }"
          placeholder="请输入备注/处理意见"
          style="margin: 20px 0"
        >
        </el-input>
      </el-form-item>
    </el-form>
  </base-btn>
</template>
<script>
import api from "@/api/index";
import workManagement from "@/api/workManagement/index";
import BaseBtn from "@/components/workflows/baseBtn";
export default {
  components: { BaseBtn },
  props: {
    id: {
      type: Number,
      default: 0
    },
    title: {
      type: String,
      default: ""
    },
    type: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      LAMValue: "",
      userName: "",
      simpleUserList: [],
      form: { target_username: null, suggestion: "" },
      rules: {
        target_username: {
          required: true,
          message: "输入转交对象",
          trigger: "change"
        }
      }
    };
  },
  created() {},
  methods: {
    querySearchAsync(queryString, cb) {
      if (!queryString) queryString = "";
      api.getSimpleUser(queryString).then(res => {
          this.simpleUserList = res.data.value;
          this.simpleUserList.forEach(item => {
            if(item){
              let deptList = item.user_dept_info_list.map(obj => {
                return obj.name;
              });
              item.value = `${item.alias}(${
                      item.username
              })【${deptList.toString()}】`;
            }
          });
        cb(this.simpleUserList);
      });
    },
    handleSelect(item) {
      if (item) {
        this.form.target_username = item.username;
      }
    },
    // 留言
    toDeliver(callback) {
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          if (this.title.indexOf("转交") != -1) {
            if (this.title == "强制转交") {
              this.form.from_admin = 1;
            }
            workManagement.toDeliver(this.id, this.form).then(res => {
              this.successCallBack(res);
              callback && callback();
            });
          } else if (this.title == "加签") {
            workManagement.toAddNode(this.id, this.form).then(res => {
              this.successCallBack(res);
              callback && callback();
            });
          }
        } else {
          return false;
        }
      });
    },
    successCallBack(res) {
      this.msgSuccess(this.title + "成功");
      this.$emit("reload");
    },
    resetForm() {
      this.$refs["ruleForm"].resetFields();
    }
  }
};
</script>
<style scoped>
.el-form-item {
  margin-bottom: 0px;
}
</style>
